<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\MkWordService;
use Illuminate\Http\Request;

class MkWordController extends BaseController
{
    //功能库

    public function __construct(MkWordService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->getOption();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //列表
    public function getIndex(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('page', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function add(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['cn'] = $request->post('title', '');
        $pas['pid'] = $request->post('pid', 0);
        if (!$pas['cn'] && !$pas['pid']) {
            return $this->toJson(-2, '没有功能名');
        }
        if (!$pas['cn'] && $pas['pid']) {
            return $this->toJson(-2, '没有字段名');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $pas['cn'] = $request->post('title', '');
        $pas['pid'] = $request->post('pid', 0);
        if (!$pas['cn'] && !$pas['pid']) {
            return $this->toJson(-2, '没有功能名');
        }
        if (!$pas['cn'] && $pas['pid']) {
            return $this->toJson(-2, '没有字段名');
        }
        $res = $this->db->modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}